package edu.upf.da.samples.datagram.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class ChatCliente {
	private DatagramSocket socketCliente = null;
	private DatagramPacket paqueteEnvio;

	public ChatCliente() {
		super();
	}

	public void cliente() {
		try {
			socketCliente = new DatagramSocket();
			InetAddress DireccionHostServidor = InetAddress.getByName("localhost");
			BufferedReader entradaUsuario = new BufferedReader(new InputStreamReader(System.in));
			while (socketCliente != null) {
				String lineaEntrada = entradaUsuario.readLine();
				if (lineaEntrada == null || lineaEntrada.equals(""))
					return;
				byte datosDatagrama[] = lineaEntrada.getBytes();
				paqueteEnvio = new DatagramPacket(datosDatagrama,
						datosDatagrama.length, DireccionHostServidor, 6992);
				socketCliente.send(paqueteEnvio);
			}
		} catch (SocketException se) {
			se.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

	}

	/**
	 * Inicia la aplicaci�n.
	 * @param args una matriz de argumentos de l�nea de mandatos
	 */
	public static void main(java.lang.String[] args) {
		ChatCliente chat = new ChatCliente();
		chat.cliente();
	}
}
